package com.example.personalcenter;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.widget.LinearLayout;

import com.example.personalcenter.Tool.OKHttpTool;
import com.example.personalcenter.fragment.HelpFragment;
import com.example.personalcenter.fragment.HomeFragment;
import com.example.personalcenter.fragment.MeFragment;
import com.example.personalcenter.fragment.NewsFragment;
import com.example.personalcenter.fragment.ServlerFragment;
import com.example.personalcenter.onlyPerson.LoginActivity;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {
    private LinearLayout page;
    private BottomNavigationView btnPage;
    private HomeFragment homeFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();



        homeFragment = new HomeFragment();
        HelpFragment helpFragment = new HelpFragment();
        MeFragment meFragment = new MeFragment();
        NewsFragment newsFragment = new NewsFragment();
        ServlerFragment servlerFragment = new ServlerFragment();


        isToken();


        getSupportFragmentManager().beginTransaction().replace(R.id.page, homeFragment).commit();

        btnPage.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.home:
                        getSupportFragmentManager().beginTransaction().replace(R.id.page, homeFragment).commit();
                        break;
                    case R.id.server:
                        getSupportFragmentManager().beginTransaction().replace(R.id.page,servlerFragment).commit();
                        break;
                    case R.id.help:
                        getSupportFragmentManager().beginTransaction().replace(R.id.page,helpFragment).commit();
                        break;
                    case R.id.news:
                        getSupportFragmentManager().beginTransaction().replace(R.id.page,newsFragment).commit();
                        break;
                    case R.id.me:
                        getSupportFragmentManager().beginTransaction().replace(R.id.page,meFragment).commit();
                        break;
                }
                return true;
            }
        });
    }

    void isToken() {
        // 获取 SharedPreferences 对象
        SharedPreferences sharedPreferences = getSharedPreferences("user_data", Context.MODE_PRIVATE);
        // 从 SharedPreferences 中检索请求头/令牌
        String authToken = sharedPreferences.getString("auth_token", null);
        OKHttpTool.TOKEN = authToken;
        if (TextUtils.isEmpty(authToken)) {
            //跳转
            Intent intent = new Intent(MainActivity.this,LoginActivity.class);
            startActivity(intent);
            finish();
        }else{
            String isToMeFragment = getIntent().getStringExtra("isToMeFragment");
            if ("y".equals(isToMeFragment)) {
                getSupportFragmentManager().beginTransaction().replace(R.id.page, homeFragment).commit();
            }
        }
    }

    private void initView() {

        page = findViewById(R.id.page);
        btnPage = findViewById(R.id.btn_page);

    }
}